package com.zoweunion.mechanic.controller.order;

import com.alibaba.fastjson.JSONObject;
import com.zoweunion.mechanic.controller.BaseController;
import com.zoweunion.mechanic.model.ResponseBean;
import com.zoweunion.mechanic.model.User;
import com.zoweunion.mechanic.plugins.websocket.WebSocketServer;
import com.zoweunion.mechanic.service.app.AppService;
import com.zoweunion.mechanic.service.impl.TSBrOrderDetailRepairmanServiceImpl;
import com.zoweunion.mechanic.service.order.KFOrderService;
import com.zoweunion.mechanic.service.order.TZOrderService;
import com.zoweunion.mechanic.util.MyException;
import com.zoweunion.mechanic.util.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 听诊工单控制层
 * @author Administrator
 */
@RestController
@RequestMapping("/tzOrder")
@SuppressWarnings("unchecked")
public class TZOrderController extends BaseController{

	@Autowired
	private TZOrderService tZOrderService;

	@Autowired
	private TSBrOrderDetailRepairmanServiceImpl repairmanService;
	@Autowired
	private KFOrderService kFOrderService;
	@Autowired
	private AppService appService;

	/**
	 * 获取待听诊列表(分页,条件模糊,排序)
	 * @throws MyException
	 */
	@PostMapping("/dtzList")
	public ResponseBean dtzList(@RequestBody String str,HttpServletRequest request) throws MyException{
		logBefore(logger, "听诊工单_待听诊列表");
		Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
		User user = getCurrentUser(request);
		reqMap.put("user", user);
		reqMap.put("s_id", getCurrentUser(request).getS_id());
		reqMap.put("order_statuses",new int[]{Constants.ORDER_STATUS_DTZ});
		//如果当前用户不是管理员 就要根据登陆者id查询列表
		if(!getCurrentUser(request).getR_id().equals(Constants.ROLE_GLY)) {
			reqMap.put("u_t_id", getCurrentUser(request).getId());
		}
		Map<String, Object> resultMap=tZOrderService.listByStatus(reqMap);
		logAfter(logger);
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",resultMap);
	}


	/**
	 * 获取历史工单列表(分页,条件模糊,排序)
	 * @throws MyException
	 */
	@PostMapping("/tzHistoryList")
	public ResponseBean tzHistoryList(@RequestBody String str,HttpServletRequest request) throws MyException{
		logBefore(logger, "听诊工单_历史工单");
		Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
		User user = getCurrentUser(request);
		reqMap.put("user", user);
		reqMap.put("s_id", getCurrentUser(request).getS_id());
		reqMap.put("history","history");//标志位 代表当前是查询历史
		//创建需要查询的状态数组
		int[]order_statuses={Constants.ORDER_STATUS_YTZ,Constants.ORDER_STATUS_PJ,Constants.ORDER_STATUS_DLL,Constants.ORDER_STATUS_YLL,
				Constants.ORDER_STATUS_QWXC,Constants.ORDER_STATUS_DDXC,Constants.ORDER_STATUS_QRFA,Constants.ORDER_STATUS_ZJ,Constants.ORDER_STATUS_HJ,
				Constants.ORDER_STATUS_WXJS,Constants.ORDER_STATUS_QRJS,Constants.ORDER_STATUS_DZF,Constants.ORDER_STATUS_QRSK,Constants.ORDER_STATUS_DPJ,Constants.ORDER_STATUS_WC,
				Constants.ORDER_STATUS_TZWC
				};
		reqMap.put("order_statuses",order_statuses);
		//如果当前用户不是管理员 就要根据登陆者id查询列表
		if(!getCurrentUser(request).getR_id().equals(Constants.ROLE_GLY)) {
			reqMap.put("u_t_id", getCurrentUser(request).getId());
		}
		Map<String, Object> resultMap=tZOrderService.listByStatus(reqMap);
		logAfter(logger);
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",resultMap);
	}

	/**
	 * 听诊操作
	 * @throws MyException
	 */
	@PostMapping("/doListen")
	public ResponseBean doListen(@RequestBody String str,HttpServletRequest request) throws MyException{
		logBefore(logger, "听诊工单_听诊操作");
		Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("s_id", getCurrentUser(request).getS_id());
		reqMap.put("current_user_id", getCurrentUser(request).getId());

		// 这里先删除之前的那个修工，省得后面车主完成的时候报错
		Map<String,Object> deleteXgMap = new HashMap<>();
		deleteXgMap.put("order_id",reqMap.get("order_id"));
		repairmanService.removeByMap(deleteXgMap);

		// 自动分配仓库,先分配仓库，后面更新时消息推送只推送给对应的仓库
		kFOrderService.autoOrderWareHouse(reqMap);

		//调用客服工单service的更新工单方法
		int result=kFOrderService.updateOrderInfo(reqMap);



		if(result<=0) {
			return new ResponseBean(Constants.CODE_HANDEL_FAILED,"操作失败",null);
		}
		logAfter(logger);
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",null);
	}

	/**
	 * 自动听诊操作
	 * @throws MyException
	 */
	@PostMapping("/autoDoListen")
	public ResponseBean autoDoListen(@RequestBody String str,HttpServletRequest request) throws MyException{

		logBefore(logger, "自动听诊工单_听诊操作");
		Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("s_id", getCurrentUser(request).getS_id());
		reqMap.put("current_user_id", getCurrentUser(request).getId());

		// 这里自动分配修工人员的的userId,匹配给repairmanInfo,前端不用传了
		reqMap.put("currentRoleId",getCurrentUser(request).getR_id());
		reqMap.put("searchRoleId",Constants.ROLE_XG);
		String xgUserId = kFOrderService.autoGetXgUserId(reqMap);
		if(xgUserId==null || xgUserId.equals("")){
			return new ResponseBean(Constants.CODE_HANDEL_FAILED, "自动获取修工失败", null);
		}

		// 这里先删除之前的那个修工，省得后面车主完成的时候报错
		Map<String,Object> deleteXgMap = new HashMap<>();
		deleteXgMap.put("order_id",reqMap.get("order_id"));
		repairmanService.removeByMap(deleteXgMap);

		List<Map<String,Object>> repairmanList = new ArrayList<>();
		Map<String,Object> repairMan = new HashMap<>();
		repairMan.put("u_type",1);
		repairMan.put("u_id",xgUserId);
		repairmanList.add(repairMan);
		reqMap.put("repairmanInfo",repairmanList);

		// 自动分配仓库,先分配仓库，后面更新时消息推送只推送给对应的仓库
		kFOrderService.autoOrderWareHouse(reqMap);

		//调用客服工单service的更新工单方法
		int result=kFOrderService.updateOrderInfo(reqMap);


		if(result<=0) {
			return new ResponseBean(Constants.CODE_HANDEL_FAILED,"操作失败",null);
		}
		logAfter(logger);
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",null);
	}

	/**
	 * 转诊操作
	 * @throws MyException
	 */
	@PostMapping("/changeTZ")
	public ResponseBean changeTZ(@RequestBody String str,HttpServletRequest request) throws MyException{
		logBefore(logger, "听诊工单_转诊");
		Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("current_user_id", getCurrentUser(request).getId());
		//调用客服工单service的更新工单方法
		int result=kFOrderService.updateOrderInfo(reqMap);
		if(result<=0) {
			return new ResponseBean(Constants.CODE_HANDEL_FAILED,"操作失败",null);
		}
		Map<String,String> strMap=new HashMap<>();
		strMap.put("type","3");
		strMap.put("data","车主提交工单，请尽快受理");
		strMap.put("code","10000");
		new WebSocketServer().sendtoUser(JSONObject.toJSONString(strMap),reqMap.get("u_t_id").toString());
		logAfter(logger);
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",null);
	}

	/**
	 * 发起返修(听诊历史工单中状态为已完成的订单才能发起返修)
	 * @param str  order_id=原工单id  order_status=返修原因  repairmanInfo=修工信息
	 */
	@PostMapping("/doRepair")
	public ResponseBean doRepair(@RequestBody String str,HttpServletRequest request) throws MyException{
		logBefore(logger, "听诊工单_发起返修");
		Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("s_id", getCurrentUser(request).getS_id());
		reqMap.put("current_user_id", getCurrentUser(request).getId());
		int result=tZOrderService.doRepair(reqMap);
		if(result<=0) {
			return new ResponseBean(Constants.CODE_HANDEL_FAILED,"操作失败",null);
		}
		logAfter(logger);
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",null);
	}
}